package LC;

/**
 * https://leetcode.com/problems/distinct-subsequences/description/
 * Given a string S and a string T, count the number of distinct subsequences of S which equals T.
 * A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).
 * Here is an example:
 * S = "rabbbit", T = "rabbit"
 * Return 3.
 */
public class LC_115_DistinctSubsequences_String_DP {

    public static void main(String[] args) {
        int n = Solution.numDistinct("rabbbit", "rabbit");
        System.out.println(n);
    }

    static class Solution {
        static int numDistinct(String s, String t) {
            int row = s.length();
            int col = t.length();
            if (row <= 0 || col <= 0)
                return 0;

            int[][] dp = new int[row][col];
            char[] chars = s.toCharArray();
            char[] chart = t.toCharArray();

            dp[0][0] = chars[0] == chart[0] ? 1 : 0;
            for (int i = 1; i < row; i++) {
                dp[i][0] = dp[i - 1][0];
                if (chars[i] == chart[0])
                    dp[i][0] += 1;
            }
            for (int i = 1; i < row; i++) {
                for (int j = 1; j < col; j++) {
                    dp[i][j] = dp[i - 1][j];
                    if (chars[i] == chart[j])
                        dp[i][j] += dp[i - 1][j - 1];
                }
            }
            return dp[row - 1][col - 1];
        }
    }

}